<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function arrange(name) {
      const fnCallbacks = []
      return {
        dosomething() {
          fnCallbacks.push(() => {
            console.log('dosomething');
          })
          return this
        },
        wait(timer) {
          fnCallbacks.push(() => {
            return new Promise(resolve => {
              setTimeout(() => {
                console.log(`等待${timer}秒`);
                resolve()
              }, timer * 1000)
            })
          })
          return this
        },
        waitFirst(timer) {
          fnCallbacks.unshift(() => {
            return new Promise(resolve => {
              setTimeout(() => {
                console.log(`等待${timer}秒`);
                resolve()
              }, timer * 1000)
            })
          })
          return this
        },
        async execute() {
          for(const cb of fnCallbacks) {
            await cb()
          }
        },
      }
    }
    console.log(arrange());
    arrange('william')
      .wait(5)
      .dosomething('commit')
      .waitFirst(3)
      .execute()
  </script>
</body>

</html>